package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import contacts614.po.Room;
import contacts614.po.Student;
import controller.StudentControllerListener;

/**
 * 框架
 * 
 * @author 15375
 *
 */
public class StudentFrame extends JFrame {
	private StudentControllerListener studentControllerListener;
	private JPanel tablePanel;
	private int selRow = -1;
	private String studentNo;

	public StudentFrame() {
		this.studentControllerListener = new StudentControllerListener();
		this.studentControllerListener.setStudentFrame(this);
		this.setJMenuBar(getContactsMenuBar());
		this.add(this.getContactsModifyToolBar(), BorderLayout.NORTH);// 东西南北中布局
		List<Student> list = studentControllerListener.getStudentService().findAllStudent();
		System.out.println(list.size());
		this.add(this.getContactsTablePanel(list), BorderLayout.CENTER);
		this.setVisible(true);
		this.setSize(1000, 1000);// 最外边的框架
		// this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.validate();
	}

	private JToolBar getContactsModifyToolBar() {
		// TODO Auto-generated method stub
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);// 是否移动工具栏
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);// 设置对齐方式
		JButton addStudentButton = new JButton("添加");
		JButton searchStudentButton = new JButton("查询");
		JButton updateStudentButton = new JButton("修改");
		JButton deleteStudentButton = new JButton("删除");
		addStudentButton.addActionListener(studentControllerListener);
		searchStudentButton.addActionListener(studentControllerListener);
		updateStudentButton.addActionListener(studentControllerListener);
		deleteStudentButton.addActionListener(studentControllerListener);
		addStudentButton.setActionCommand("addStudentDialog");
		searchStudentButton.setActionCommand("searchStudentDialog");
		updateStudentButton.setActionCommand("updateStudentDialog");
		deleteStudentButton.setActionCommand("deleteStudent");
		toolBar.add(addStudentButton);
		toolBar.add(new JToolBar.Separator());// 分隔
		toolBar.add(searchStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(updateStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(deleteStudentButton);
		return toolBar;
	}

	/**
	 * 测试方法是否成功
	 * 
	 * @param studentList
	 */
	public void refresh(List<Student> studentList) {
		System.out.println("测试——当前学生数量为：" + studentList.size());
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(studentControllerListener.getStudentService().findAllStudent()),
				BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功!");
	}

	private JPanel getContactsTablePanel(List<Student> studentList) {
		tablePanel = new JPanel();
		String titles[] = { "studentNo", "studentName", "room", "studentBirth", "studentTelephone", "studentEmail",
				"studentAddress" };
		Object data[][] = new Object[studentList.size()][7];

		Iterator<Student> iter = studentList.iterator();// 迭代器
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStudentNo();
			data[i][1] = student.getStudentName();
			data[i][2] = student.getRoom().getRoomNo();
			data[i][3] = student.getStudentBirth();
			data[i][4] = student.getStudentTelephone();
			data[i][5] = student.getStudentEmail();
			data[i][6] = student.getStudentAddress();
			i++;
		}

		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));// 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));// 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30));// 表头高度
		table.setRowHeight(table.getRowHeight() + 15);// 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 只能选一行
		table.setPreferredScrollableViewportSize(new Dimension(900, 900));// 可视窗口的首选大小
		table.repaint();// 重绘此组件
		tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {// 该方法得到的是真实的表，不是数据库的表
				selRow = table.getSelectedRow();// 得到选中的行
				studentNo = table.getValueAt(selRow, 0).toString().trim();// 返回 selRow 和索引为 0 位置的单元格值
				String studentName = table.getValueAt(selRow, 1).toString().trim();
				String roomNo = table.getValueAt(selRow, 2).toString().trim();
				String studentBirth = table.getValueAt(selRow, 3).toString().trim();
				String studentTelephone = table.getValueAt(selRow, 4).toString().trim();
				String studentEmail = table.getValueAt(selRow, 5).toString().trim();
				String studentAddress = table.getValueAt(selRow, 6).toString().trim();
				System.out.println("studentNo=" + studentNo + "studentName=" + studentName + "roomNo=" + roomNo
						+ "studentBirth=" + studentBirth + "studentTelephone=" + studentTelephone + "studentEmail="
						+ studentEmail + "studentAddress" + studentAddress);
			}
		});
		return tablePanel;
	}

	private JMenuBar getContactsMenuBar() {
		JMenuItem addStudentMenuItem = new JMenuItem("添加学生信息");
		JMenuItem listStudentMenuItem = new JMenuItem("学生列表");
		addStudentMenuItem.addActionListener(studentControllerListener);
		addStudentMenuItem.setActionCommand("addStudentDialog");
		listStudentMenuItem.addActionListener(studentControllerListener);
		listStudentMenuItem.setActionCommand("refreshStudentFrame");
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("学生管理");
		fileMenu.add(addStudentMenuItem);
		fileMenu.add(listStudentMenuItem);
		bar.add(fileMenu);
		return bar;
	}

	public void showSearchResult(List<Student> result) {
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功");
	}

	public StudentControllerListener getStudentControllerListener() {
		return studentControllerListener;
	}

	public JPanel getTablePanel() {
		return tablePanel;
	}

	public int getSelRow() {
		return selRow;
	}

	public String getStudentNo() {
		return studentNo;
	}

}
